package com.huang.server;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserDetailServiceImp implements UserDetailsService {


    @Resource
    private BCryptPasswordEncoder encoder;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //基于数据库查询用户数据
        if (!"jake".equals(username)) {
            throw new UsernameNotFoundException("用户不存在");
        }
        //基于数据库查询的用户密码和权限
        String encodepwd=encoder.encode("1111");
        List<GrantedAuthority> grantedAuthorities = AuthorityUtils.commaSeparatedStringToAuthorityList("sys:res:retrieve,sys:res:create");
        User user=new User(username, encodepwd, grantedAuthorities);

        return user;
    }
}
